package com.hdyg.sixg.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.allen.library.SuperTextView;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.activity.mine.AddressActivity;
import com.hdyg.sixg.adapter.PayMethodAdapter;
import com.hdyg.sixg.entry.BuyMiniBean;
import com.hdyg.sixg.mvp.Contrant.CBuyMini;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.base.SpMsg;
import com.hdyg.sixg.mvp.presenter.PBuyMini;
import com.hdyg.sixg.util.PopWindowUtil;
import com.hdyg.sixg.util.StringUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import java.util.List;
import butterknife.BindView;

/**
 * @author CZB
 * @describe 购买矿机
 * @time 2019/4/19 10:34
 */
public class BuyMiniChildActivity extends BaseActivity implements CBuyMini.IVBuyMini {

    private static final int REQUEST_CODE = 0x00001;

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.sv_address)
    SuperTextView svAddress;
    @BindView(R.id.sv_detail)
    SuperTextView svDetail;
    @BindView(R.id.sv_paymethod)
    SuperTextView svPaymethod;
    @BindView(R.id.et_trans_pwd)
    EditText etTransPwd;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.bt_pay)
    Button btPay;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private CBuyMini.IPBuyMini mPresenter;
    private String id,trapassword,payMethodId;
    private BuyMiniBean dataBean;
    private List<BuyMiniBean.PayWayBean> payWayBeans;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_buy_mini_child;
    }

    @Override
    protected void initView() {
        tvTopTitle.setText(R.string.buy_mini_title);
        onClickListener();
        id = getIntent().getStringExtra(SpMsg.INTENT_STR);
    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            initData();
        });
        llTopLeft.setOnClickListener(v -> finish());
        //添加收货地址
        svAddress.setCenterTvClickListener(() -> {
            mBundle = new Bundle();
            mBundle.putInt(SpMsg.INTENT_CODE, AddressActivity.ADDRESS_SAVE);
            startActivityForResult(AddressActivity.class, mBundle, REQUEST_CODE);
        });
        //支付方式
        svPaymethod.setOnSuperTextViewClickListener(superTextView -> {
            showPayMethodPop(superTextView);
        });
        //支付
        btPay.setOnClickListener(v -> {
            trapassword = etTransPwd.getText().toString().trim();
            if (TextUtils.isEmpty(payMethodId)){
                toastNotifyShort(R.string.toast_pay_method);
                return;
            }
            if (TextUtils.isEmpty(trapassword)){
                toastNotifyShort(R.string.regist_trans_pwd_hint);
                return;
            }
            if (dataBean == null) {
                return;
            }
            if (StringUtil.isFastDoubleClick()){
                return;
            }
            mPresenter.pGetPay(BaseUrl.BUY_MINI_SUBMIT_URL, GetParamUtil.getBuyMiniSubmitParam(dataBean.getId(),trapassword,payMethodId));
        });
    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.BUY_MINI_URL, GetParamUtil.getBuyMiniParam(id));
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PBuyMini(this);
    }

    //显示支付方式悬浮窗
    private void showPayMethodPop(View v) {
        if (payWayBeans == null) {
            toastNotifyShort(R.string.toast_pay_method);
            return;
        }
        View view = View.inflate(mContext, R.layout.pop_cash_bi_coin, null);
        RecyclerView rvArea = view.findViewById(R.id.rv_coin);
        rvArea.setLayoutManager(new LinearLayoutManager(mContext));
        PayMethodAdapter areaAdapter = new PayMethodAdapter(R.layout.item_cash_bi_coin, payWayBeans);
        rvArea.setAdapter(areaAdapter);
        areaAdapter.setOnItemClickListener((adapter, view1, position) -> {
            payMethodId = payWayBeans.get(position).getPayid();
            svPaymethod.setRightString(payWayBeans.get(position).getName());
            String sum = String.format(getResources().getString(R.string.mini_child_money), payWayBeans.get(position).getTotal_price());
            tvMoney.setText(sum);
            mPopupWindow.dismiss();
        });
        mPopupWindow = PopWindowUtil.getInstance().makePopupWindow(mContext, view, ViewGroup.LayoutParams.MATCH_PARENT)
                .showAsDropDown(mContext, v, 0);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_CODE:
                initData();
                break;
        }
    }

    @Override
    public void vGetInitSuccess(BuyMiniBean dataBean) {
        this.dataBean = dataBean;
        this.payWayBeans = dataBean.getPayway();
        if ("1".equals(dataBean.getIs_bind())) {
            //已绑定地址
            svAddress.setLeftTopString(dataBean.getName())
                    .setLeftBottomString(dataBean.getAddress())
                    .setRightTopString(dataBean.getPhone())
                    .setCenterString("");
        } else {
            //未绑定地址
            svAddress.setLeftTopString("")
                    .setLeftBottomString("")
                    .setRightTopString("")
                    .setCenterString(getResources().getString(R.string.mini_child_add_address));
        }
//        String percent = StringUtil.getPercentage(Float.valueOf(dataBean.getRate()), 100);
        svDetail.setRightTopString(dataBean.getSize())
                .setRightString(dataBean.getPrice());
//                .setRightBottomString(percent);
        payMethodId = "";
        svPaymethod.setRightString("");
        tvMoney.setText("");
    }

    @Override
    public void vGetPaySuccess(String dataBean) {
        toastNotifyShort(dataBean);
        setResult(RESULT_OK);
        finish();
    }
}
